﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Input;
using VolunteerManager.Model;

namespace VolunteerManager.ViewModel
{
    /// <summary>
    /// 导入时使用
    /// </summary>
    public class ImportViewModel : ViewModelBase
    {
        public ImportViewModel()
        {

        }

        private ICommand _selectcommand;
        private ICommand _saveCommand;

        public string SelectedFile { get; set; }
        public DataTable ImportDate { get; set; }

        public ICommand SelectCommand
        {
            get
            {
                if (_selectcommand == null)
                {
                    _selectcommand = new RelayCommand(() => select());
                }

                return _selectcommand;
            }
        }

        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(() => save());
                }

                return _saveCommand;
            }
        }

        private void testVotter()
        {
            Voluteer v = new Voluteer();
            v.Name = "xxx";
            v.Wechat = " xxxxx";
            v.QQ = "213432432";
            v.Phone = "fdsa";
            v.JoinDate = DateTime.Now;
            v.Birth = new DateTime(1990, 12, 12);
            v.Email = "fdsaf@sina.com";
            v.Peoples = Man.回;
            v.Sex = Sex.男;

            v.Save();
        }

        /// <summary>
        /// 解析日期格式
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        private DateTime _parseDate(string v)
        {
            int year = DateTime.Now.Year;
            int month = 1;
            int day = 1;
            if (v.Length > 7)
            {
                string reg = @"(\d{4})年(\d{1,2})月(\d{1,2})日";
                if (Regex.IsMatch(v, reg))
                {
                    MatchCollection matches = Regex.Matches(v, reg);
                    if (matches.Count >0)
                    {
                        year =int.Parse(matches[0].Groups[1].ToString());
                        month = int.Parse(matches[0].Groups[2].ToString());
                        day = int.Parse(matches[0].Groups[3].ToString());

                    }
                }
            }
            else if (v.Length > 5)
            {
                string reg = @"(\d{4})年(\d{1,2})月";
                if (Regex.IsMatch(v, reg))
                {
                    MatchCollection matches = Regex.Matches(v, reg);
                    if (matches.Count > 0)
                    {
                        year = int.Parse(matches[0].Groups[1].ToString());
                        month = int.Parse(matches[0].Groups[2].ToString());

                    }
                }

            }
            else if(v.Length > 4)
            {
                year = int.Parse(v.Substring(0, 4));
            }
            return new DateTime(year, month, day);

        }

        private void save()
        {
            int count = 0;
            int errorcount = 0;
            if (ImportDate == null || ImportDate.Columns.Count < 14)
            {
                System.Windows.MessageBox.Show("数据格式与模板不符，请查正", "系统提示", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                return;
            }

            foreach (DataRow row in ImportDate.Rows)
            {
                //判断是否为空行
                if (row[0].ToString() == "" && row[1].ToString() == "" && row[2].ToString() == "")
                {
                    continue;
                }

                Voluteer v = new Voluteer();
                v.Wechat = row[0].ToString();
                v.Name = row[1].ToString();
                v.Sex = (Sex) Enum.Parse(typeof(Sex), row[2].ToString());

                v.Peoples = (Man)Enum.Parse(typeof(Man), row[3].ToString());
                v.Regions = row[4].ToString();
                v.JoinDate = _parseDate(row[5].ToString());
                v.Birth = _parseDate(row[6].ToString());

                v.Phone = row[7].ToString();
                v.QQ = row[8].ToString();
                v.Email = row[9].ToString();
                v.Professional = row[10].ToString();
                v.Vocation = row[11].ToString();
                v.Forte = row[12].ToString();
                v.Models = row[13].ToString();

                if (v.Save() > 0)
                {
                    count++;
                }
                else
                {
                    errorcount++;
                }
            }
            System.Windows.MessageBox.Show("已导入 " + count　+ "条", "系统提示");
        }

        private void select()
        {
            Microsoft.Win32.OpenFileDialog od = new Microsoft.Win32.OpenFileDialog();
            od.Filter = "(excel文件)|*.xlsx";

            if (od.ShowDialog().Value)
            {
                SelectedFile = od.FileName;
                RaisePropertyChanged("SelectedFile");

                DataTable dat = new DataTable();
                string error = "";
                
                AsposeExcelTool.ExcelFileToDataTable(SelectedFile, out dat, out error);
                if (error == "")
                {
                    dat.Rows[0].Delete();   //删除第一行，标题

                    DataRow titles = dat.Rows[0];
                    for (int i = 0; i < dat.Columns.Count; i++)
                    {

                        dat.Columns[i].ColumnName = titles[i].ToString();
                    }

                    dat.Rows[0].Delete();

                    ImportDate = dat;

                    RaisePropertyChanged("ImportDate");
                }
            }
        }
    }
}
